from datetime import datetime

from . import db


# 商品类
class Commod(db.Model):
    __tablename__ = 'commods'

    id = db.Column(db.Integer, primary_key=True)
    # 商品名字
    com_name = db.Column(db.String(32), unique=True, nullable=False)
    # 商品标题
    com_title = db.Column(db.String(128), nullable=False)
    # 商品价格
    com_price = db.Column(db.Integer, nullable=False, default=0)
    # 商品描述
    com_desc = db.Column(db.String(512), nullable=False)

    # 商品图片
    commod_photos = db.relationship("CommodPhotos", lazy='dynamic', backref="commods")

    # 商品分类
    commod_category_id = db.Column(db.Integer, db.ForeignKey("commod_category.id"))
    commod_category = db.relationship('CommodCategory', backref='commods')


# 商品图片表
class CommodPhotos(db.Model):
    __tablename__ = "commod_photos"

    id = db.Column(db.Integer, primary_key=True)

    # 商品照片路径
    Photo_url = db.Column(db.String(256))

    # 图片的用途
    Photo_use = db.Column(db.Integer, default=1)
    # 1 用来在主页中显示
    # 2 用来在商品详情页显示
    # 3 用来在商品描述显示
    # 4 用来在购物车中显示

    # 当前商品的id
    commod_id = db.Column(db.Integer, db.ForeignKey("commods.id"))
    # commod = db.relationship('Commod', backref=db.backref('photo', lazy='dynamic'))
    # 一对多 一个商品对应多个图片


# 商品分类
class CommodCategory(db.Model):
    """商品分类"""
    __tablename__ = "commod_category"

    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(64), nullable=False)  # 分类名
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间


# 购物车表
class Com_car(db.Model):
    __tablename__ = "com_car"
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), primary_key=True)  # 用户编号
    comd_id = db.Column(db.Integer, db.ForeignKey("commods.id"), primary_key=True)  # 商品编号
    create_time = db.Column(db.DateTime, default=datetime.now)  # 收藏加入时间
    comd_count = db.Column(db.Integer, default=0)  # 加入购物车的次数


# 用户表
class Users(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)  # 用户编号
    nick_name = db.Column(db.String(32), unique=True, nullable=False)  # 用户昵称
    password_hash = db.Column(db.String(128), nullable=False)  # 加密的密码
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 手机号
    avatar_url = db.Column(db.String(256))  # 用户头像路径
    create_time = db.Column(db.DateTime, default=datetime.now)  # 注册时间
    is_admin = db.Column(db.Boolean, default=False)
    gender = db.Column(  # 性别
        db.Enum(
            "MAN",  # 男
            "WOMAN"  # 女
        ),
        default="MAN"
    )

    # 购物车
    com_car = db.relationship("Commod",
                              secondary=Com_car.__table__,
                              backref=db.backref('collected_user', lazy='dynamic'),
                              lazy='dynamic')

    # 订单
    orders = db.relationship("Orders", lazy='dynamic', backref="user")

    # 地址

    addrs = db.relationship("Addrs", lazy="dynamic", backref="user")

    # 买过的商品
    buy = db.relationship("Buy", lazy="dynamic", backref="user")


# 评论
class Comment(db.Model):
    """评论"""
    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True)  # 评论编号

    com_id = db.Column(db.Integer, db.ForeignKey("commods.id"), nullable=False)  # 商品id
    content = db.Column(db.Text, nullable=False)  # 评论内容
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间

    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)  # 用户id
    user = db.relationship("Users", backref="comments")


# 订单表
class Orders(db.Model):
    __tablename__ = "orders"

    id = db.Column(db.Integer, primary_key=True)
    # 订单号
    order_id = db.Column(db.String(128), nullable=False)
    # 用户id
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)  # 用户编号
    # 商品id字符串列表
    comd_list = db.Column(db.String(128), nullable=False)  # “1，2，3，4，5，6”
    # 订单金额
    order_sum = db.Column(db.String(64), nullable=False)
    # 订单创建时间
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 订单状态
    order_status = db.Column(db.Enum(
        "1",  # 未支付
        "2",  # 待发货
        "3",  # 已发货
        "4",  # 派送中
        "5",  # 以送达
        "6",  # 订单生成中
    ),
        default="6")


# 省市区三级联动地址表
class areas(db.Model):
    __tablename__ = "areas"

    id = db.Column(db.Integer, primary_key=True)

    p_id = db.Column(db.Integer)

    addr_name = db.Column(db.String(128))

    c_id = db.Column(db.Integer)


class Addrs(db.Model):
    __tablename__ = "user_addr"

    id = db.Column(db.Integer, primary_key=True)

    # 所在省
    province = db.Column(db.String(24), nullable=False)
    # 所在市
    city = db.Column(db.String(24), nullable=False)
    # 所在县
    county = db.Column(db.String(24))
    # 具体位置
    definite = db.Column(db.String(124), nullable=False)
    # 名字
    name = db.Column(db.String(24))
    # 称呼
    call = db.Column(db.String(24))
    # 联系手机号
    phone = db.Column(db.String(13))
    # 创建时间
    create = db.Column(db.DateTime, default=datetime.now())
    # 关联用户
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)


class Buy(db.Model):
    # 买过的商品表
    __tablename__ = "buys"

    id = db.Column(db.Integer, primary_key=True)
    # 商品id
    com_id = db.Column(db.Integer, nullable=False)
    # 购买数量
    count = db.Column(db.Integer, default=0)
    # 对应的订单id
    order_id = db.Column(db.Integer, nullable=False)
    # 用户id
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
